<?php

namespace Loop;


require_once(PATH_LOOP . 'Model/CTestObjectA.php');
require_once(PATH_LOOP . 'Model/CTestObjectB.php');
require_once(PATH_LOOP . 'Model/CTestObjectC.php');
require_once(PATH_LOOP . 'Model/CTestObjectD.php');
require_once(PATH_LOOP . 'Model/CTestObjectE.php');

/**
 * @package		Loop
 * @category	Class
 */
 
class CObject_Test extends \UnitTestCase
{

	/**
	 *
	 */

	public function testSave()
	{
		// Simple test

		$objectA = new CTestObjectA();
		$objectA->save();

		$rawObjects = $objectA->find();

		$this->assertEqual(count($rawObjects), 1);

		$objectA->load(1);
		$objectA->delete();


		// Test with wrong association id

		try 
		{
			$objectA = new CTestObjectA();
			$objectA->setTestObjectBId('test2');
			$objectA->save();

			$this->fail("Expected exception");
		}
		catch(\Exception $e)
		{
			 $this->pass();
		}

		$rawObjects = $objectA->find();
		$this->assertEqual(count($rawObjects), 0);



		// Test with wrong type field

		try
		{
			$objectA = new CTestObjectA();
			$objectA->setIntegerField('test');
			$objectA->save();

			$this->fail("Expected exception");
		}
		catch(\Exception $e)
		{
			 $this->pass();
		}		


		// Test with an association to create 

		$rawObjects = $objectA->find();
		$this->assertEqual(count($rawObjects), 0);


		$objectProperties = array
		(
			'testObjectATestObjectBId'=> array 
			(
				'testObjectBDescription' => 'je suis un test'
			)
		);


		$objectA = new CTestObjectA();
		$objectA->save($objectProperties);

		$rawObjects = $objectA->find();
		$this->assertEqual(count($rawObjects), 1);	
		$this->assertEqual($objectA->getTestObjectBId(), 1);	

		$objectB = new CTestObjectB();
		$this->assertEqual(count($rawObjects), 1);		

	}


	/**
	 *
	 */

	public function testSaveAs()
	{
		$objectA = new CTestObjectA();	
		$objectAClone = new CTestObjectA();
		$objectC  = new CTestObjectC();
		$objectD = new CTestObjectD();
		$objectB = new CTestObjectB();
		$objectE = new CTestObjectE();


		// Load the object

		$objectA->load(2);
		$objectA->setName('mon object');
		$objectA->save();


		// Create object e

		$objectE->load(2);
		$objectE->setName('ancester');
		$objectE->save();


		// Create object c

		$objectC->save();


		// 

		$objectB->load(1);
		$objectB->setTestObjectCId(1);
		$objectB->save();


		// Create an object D

		$objectD->setTestObjectAId(2);
		$objectD->save();


		// Object E/2 is ancester of A/2 who has association with B/1 . B/1 is associate to C/1


		// Call the function  with no recursive

		$id = $objectA->saveAs(false);


		// Load the clone 

		$objectAClone->load($id);


		$this->assertEqual($objectA->getName(), $objectAClone->getName());
		$this->assertEqual($objectA->getTestObjectBId(), '1');
		$this->assertEqual($objectAClone->getTestObjectBId(), '1' );
		$this->assertEqual($objectAClone->getTestObjectEId(), '2' );



		$objectA->load(2);

		// Call the function  with resursive

		$id = $objectA->saveAs(true);	


		// Load the clone 

		$objectAClone->load($id);

		$this->assertEqual($objectAClone->getTestObjectBId(), '2' );
		$this->assertEqual($objectAClone->getTestObjectEId(), '3' );		


		$objectB->load(2);
		$this->assertEqual($objectB->getTestObjectCId(), '2');	
	}
}

?>